#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
进度条组件示例程序
"""
import sys
import time
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
                            QHBoxLayout, QPushButton, QLabel, QSlider, QComboBox,
                            QCheckBox, QSpinBox, QGroupBox, QScrollArea)

from component import Progress, Theme, Card, VBox, HBox


class MainWindow(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("进度条组件示例")
        self.resize(800, 600)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_content = QWidget()
        scroll_layout = QVBoxLayout(scroll_content)
        scroll_layout.setAlignment(Qt.AlignTop)
        scroll_area.setWidget(scroll_content)
        main_layout.addWidget(scroll_area)
        
        # 添加基本进度条示例
        self._add_basic_progress_example(scroll_layout)
        
        # 添加不同状态的进度条示例
        self._add_status_progress_example(scroll_layout)
        
        # 添加文本内外显示的进度条示例
        self._add_text_position_example(scroll_layout)
        
        # 添加条纹进度条示例
        self._add_striped_progress_example(scroll_layout)
        
        # 添加环形进度条示例
        self._add_circle_progress_example(scroll_layout)
        
        # 添加动态进度条示例
        self._add_dynamic_progress_example(scroll_layout)
        
        # 添加自定义进度条示例
        self._add_custom_progress_example(scroll_layout)
    
    def _add_basic_progress_example(self, parent_layout):
        """添加基本进度条示例"""
        # 创建卡片
        card = Card("基本进度条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        progress_30 = Progress(value=30)
        progress_50 = Progress(value=50)
        progress_80 = Progress(value=80)
        progress_100 = Progress(value=100)
        
        # 添加到布局
        vbox.add_widget(QLabel("30% 进度"))
        vbox.add_widget(progress_30)
        vbox.add_widget(QLabel("50% 进度"))
        vbox.add_widget(progress_50)
        vbox.add_widget(QLabel("80% 进度"))
        vbox.add_widget(progress_80)
        vbox.add_widget(QLabel("100% 进度"))
        vbox.add_widget(progress_100)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_status_progress_example(self, parent_layout):
        """添加不同状态的进度条示例"""
        # 创建卡片
        card = Card("不同状态的进度条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        progress_normal = Progress(value=70, status="normal")
        progress_success = Progress(value=70, status="success")
        progress_warning = Progress(value=70, status="warning")
        progress_danger = Progress(value=70, status="danger")
        
        # 添加到布局
        vbox.add_widget(QLabel("普通状态"))
        vbox.add_widget(progress_normal)
        vbox.add_widget(QLabel("成功状态"))
        vbox.add_widget(progress_success)
        vbox.add_widget(QLabel("警告状态"))
        vbox.add_widget(progress_warning)
        vbox.add_widget(QLabel("危险状态"))
        vbox.add_widget(progress_danger)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_text_position_example(self, parent_layout):
        """添加文本内外显示的进度条示例"""
        # 创建卡片
        card = Card("文本位置")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        progress_outside = Progress(value=70, text_inside=False, show_text=True)
        progress_inside = Progress(value=70, text_inside=True, show_text=True)
        progress_no_text = Progress(value=70, show_text=False)
        progress_custom_format = Progress(value=70, text_format="已完成 {value}/100")
        
        # 添加到布局
        vbox.add_widget(QLabel("文本在外部"))
        vbox.add_widget(progress_outside)
        vbox.add_widget(QLabel("文本在内部"))
        vbox.add_widget(progress_inside)
        vbox.add_widget(QLabel("无文本"))
        vbox.add_widget(progress_no_text)
        vbox.add_widget(QLabel("自定义文本格式"))
        vbox.add_widget(progress_custom_format)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_striped_progress_example(self, parent_layout):
        """添加条纹进度条示例"""
        # 创建卡片
        card = Card("条纹进度条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        progress_striped = Progress(value=70, striped=True)
        progress_striped_success = Progress(value=70, striped=True, status="success")
        progress_striped_warning = Progress(value=70, striped=True, status="warning")
        progress_striped_danger = Progress(value=70, striped=True, status="danger")
        
        # 添加到布局
        vbox.add_widget(QLabel("普通条纹"))
        vbox.add_widget(progress_striped)
        vbox.add_widget(QLabel("成功条纹"))
        vbox.add_widget(progress_striped_success)
        vbox.add_widget(QLabel("警告条纹"))
        vbox.add_widget(progress_striped_warning)
        vbox.add_widget(QLabel("危险条纹"))
        vbox.add_widget(progress_striped_danger)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_circle_progress_example(self, parent_layout):
        """添加环形进度条示例"""
        # 创建卡片
        card = Card("环形进度条")
        
        # 创建水平布局
        hbox = HBox(margin=10, spacing=20)
        
        # 添加进度条
        progress_circle = Progress(value=70, circle=True)
        progress_circle_success = Progress(value=70, circle=True, status="success")
        progress_circle_warning = Progress(value=70, circle=True, status="warning")
        progress_circle_danger = Progress(value=70, circle=True, status="danger")
        
        # 添加到布局
        hbox.add_widget(progress_circle)
        hbox.add_widget(progress_circle_success)
        hbox.add_widget(progress_circle_warning)
        hbox.add_widget(progress_circle_danger)
        
        # 添加到卡片
        card.add_widget(hbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_dynamic_progress_example(self, parent_layout):
        """添加动态进度条示例"""
        # 创建卡片
        card = Card("动态进度条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        self.dynamic_progress = Progress(value=0)
        
        # 创建控制按钮
        control_layout = QHBoxLayout()
        
        self.start_button = QPushButton("开始")
        self.start_button.clicked.connect(self._start_progress)
        
        self.stop_button = QPushButton("停止")
        self.stop_button.clicked.connect(self._stop_progress)
        self.stop_button.setEnabled(False)
        
        self.reset_button = QPushButton("重置")
        self.reset_button.clicked.connect(self._reset_progress)
        
        control_layout.addWidget(self.start_button)
        control_layout.addWidget(self.stop_button)
        control_layout.addWidget(self.reset_button)
        
        # 创建定时器
        self.timer = QTimer(self)
        self.timer.timeout.connect(self._update_progress)
        
        # 添加到布局
        vbox.add_widget(QLabel("动态更新进度"))
        vbox.add_widget(self.dynamic_progress)
        
        control_widget = QWidget()
        control_widget.setLayout(control_layout)
        vbox.add_widget(control_widget)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _add_custom_progress_example(self, parent_layout):
        """添加自定义进度条示例"""
        # 创建卡片
        card = Card("自定义进度条")
        
        # 创建垂直布局
        vbox = VBox(margin=10, spacing=20)
        
        # 添加进度条
        self.custom_progress = Progress(value=50)
        
        # 创建控制面板
        control_group = QGroupBox("控制面板")
        control_layout = QVBoxLayout(control_group)
        
        # 进度值滑块
        value_layout = QHBoxLayout()
        value_layout.addWidget(QLabel("进度值:"))
        self.value_slider = QSlider(Qt.Horizontal)
        self.value_slider.setRange(0, 100)
        self.value_slider.setValue(50)
        self.value_slider.valueChanged.connect(self._update_custom_progress_value)
        value_layout.addWidget(self.value_slider)
        self.value_label = QLabel("50%")
        value_layout.addWidget(self.value_label)
        control_layout.addLayout(value_layout)
        
        # 状态选择
        status_layout = QHBoxLayout()
        status_layout.addWidget(QLabel("状态:"))
        self.status_combo = QComboBox()
        self.status_combo.addItems(["normal", "success", "warning", "danger"])
        self.status_combo.currentTextChanged.connect(self._update_custom_progress_status)
        status_layout.addWidget(self.status_combo)
        control_layout.addLayout(status_layout)
        
        # 文本位置
        text_layout = QHBoxLayout()
        self.show_text_check = QCheckBox("显示文本")
        self.show_text_check.setChecked(True)
        self.show_text_check.stateChanged.connect(self._update_custom_progress_text_show)
        text_layout.addWidget(self.show_text_check)
        
        self.text_inside_check = QCheckBox("文本在内部")
        self.text_inside_check.setChecked(False)
        self.text_inside_check.stateChanged.connect(self._update_custom_progress_text_inside)
        text_layout.addWidget(self.text_inside_check)
        control_layout.addLayout(text_layout)
        
        # 条纹效果
        striped_layout = QHBoxLayout()
        self.striped_check = QCheckBox("条纹效果")
        self.striped_check.setChecked(False)
        self.striped_check.stateChanged.connect(self._update_custom_progress_striped)
        striped_layout.addWidget(self.striped_check)
        control_layout.addLayout(striped_layout)
        
        # 环形进度条
        circle_layout = QHBoxLayout()
        self.circle_check = QCheckBox("环形进度条")
        self.circle_check.setChecked(False)
        self.circle_check.stateChanged.connect(self._update_custom_progress_circle)
        circle_layout.addWidget(self.circle_check)
        control_layout.addLayout(circle_layout)
        
        # 进度条宽度
        width_layout = QHBoxLayout()
        width_layout.addWidget(QLabel("进度条宽度:"))
        self.width_spin = QSpinBox()
        self.width_spin.setRange(2, 20)
        self.width_spin.setValue(6)
        self.width_spin.valueChanged.connect(self._update_custom_progress_width)
        width_layout.addWidget(self.width_spin)
        control_layout.addLayout(width_layout)
        
        # 添加到布局
        vbox.add_widget(QLabel("自定义进度条"))
        vbox.add_widget(self.custom_progress)
        vbox.add_widget(control_group)
        
        # 添加到卡片
        card.add_widget(vbox)
        
        # 添加到父布局
        parent_layout.addWidget(card)
    
    def _start_progress(self):
        """开始进度更新"""
        self.timer.start(100)  # 每100毫秒更新一次
        self.start_button.setEnabled(False)
        self.stop_button.setEnabled(True)
    
    def _stop_progress(self):
        """停止进度更新"""
        self.timer.stop()
        self.start_button.setEnabled(True)
        self.stop_button.setEnabled(False)
    
    def _reset_progress(self):
        """重置进度"""
        self.timer.stop()
        self.dynamic_progress.set_value(0)
        self.start_button.setEnabled(True)
        self.stop_button.setEnabled(False)
    
    def _update_progress(self):
        """更新进度值"""
        current_value = self.dynamic_progress.get_value()
        if current_value >= 100:
            self.timer.stop()
            self.start_button.setEnabled(True)
            self.stop_button.setEnabled(False)
            return
        
        self.dynamic_progress.set_value(current_value + 1)
    
    def _update_custom_progress_value(self, value):
        """更新自定义进度条的值"""
        self.custom_progress.set_value(value)
        self.value_label.setText(f"{value}%")
    
    def _update_custom_progress_status(self, status):
        """更新自定义进度条的状态"""
        self.custom_progress.setStatus(status)
    
    def _update_custom_progress_text_show(self, state):
        """更新自定义进度条的文本显示"""
        self.custom_progress.setShowText(state == Qt.Checked)
    
    def _update_custom_progress_text_inside(self, state):
        """更新自定义进度条的文本位置"""
        self.custom_progress.setTextInside(state == Qt.Checked)
    
    def _update_custom_progress_striped(self, state):
        """更新自定义进度条的条纹效果"""
        self.custom_progress.setStriped(state == Qt.Checked)
    
    def _update_custom_progress_circle(self, state):
        """更新自定义进度条的形状"""
        is_circle = state == Qt.Checked
        self.custom_progress = Progress(
            value=self.value_slider.value(),
            status=self.status_combo.currentText(),
            show_text=self.show_text_check.isChecked(),
            text_inside=self.text_inside_check.isChecked(),
            striped=self.striped_check.isChecked(),
            stroke_width=self.width_spin.value(),
            circle=is_circle
        )
        
        # 重新添加到布局
        parent_widget = self.sender().parent().parent()
        layout = parent_widget.layout()
        old_progress = layout.itemAt(1).widget()
        layout.replaceWidget(old_progress, self.custom_progress)
        old_progress.deleteLater()
    
    def _update_custom_progress_width(self, width):
        """更新自定义进度条的宽度"""
        self.custom_progress.setStrokeWidth(width)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 应用主题
    Theme.apply_theme(app)
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_()) 